package io.unicorn.modules.water.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.Query;

import io.unicorn.modules.water.dao.TDataErr2021Dao;
import io.unicorn.modules.water.entity.TDataErr2021Entity;
import io.unicorn.modules.water.service.TDataErr2021Service;


@Service("tDataErr2021Service")
public class TDataErr2021ServiceImpl extends ServiceImpl<TDataErr2021Dao, TDataErr2021Entity> implements TDataErr2021Service {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key   = (String)params.get("key");
        String value = (String)params.get("value");

        String dataMonth = (String) params.get("dataMonth");

        String deptId = (String)params.get("dept");
        Map<String , Object> queryMap = new HashMap<>();
        if(StringUtils.isNotBlank(deptId) && StringUtils.isNotEmpty(deptId))
        {
            String[] tmp = deptId.split("_");
            switch(tmp.length){
                case 1:
                    queryMap.put("city_name",tmp[0]);
                    break;
                case 2:
                    queryMap.put("city_name",tmp[0]);
                    queryMap.put("area_name",tmp[1]);

                    break;
                case 3:
                    queryMap.put("city_name",tmp[0]);
                    queryMap.put("area_name",tmp[1]);
                    queryMap.put("unit_name",tmp[2]);
                    break;
                case 4:
                    queryMap.put("city_name",tmp[0]);
                    queryMap.put("area_name",tmp[1]);
                    queryMap.put("unit_name",tmp[2]);
                    queryMap.put("pro_name",tmp[3]);
                    break;
            }
        }

        IPage<TDataErr2021Entity> page = this.page(
                new Query<TDataErr2021Entity>().getPage(params),
                new QueryWrapper<TDataErr2021Entity>()
                        .like(StringUtils.isNotBlank(value) && StringUtils.isNotBlank(key), key, value)
                        .allEq(queryMap!=null && queryMap.size() > 0, queryMap, false)
                        .eq(StringUtils.isNotBlank(dataMonth),"data_month",dataMonth)
        );

        return new PageUtils(page);
    }

    @Override
    public void execProGenDataErr() {
        baseMapper.execProGenDataErr();
    }

    @Override
    public void execProGenErrReport(Map<String, Object> params){ baseMapper.execProGenErrReport(params);}

    @Override
    public List<TDataErr2021Entity> queryArea(Map<String, Object> params) {
        return baseMapper.queryArea(params);
    }

    @Override
    public List<TDataErr2021Entity> queryAreaUnit(Map<String, Object> params) {
        return baseMapper.queryAreaUnit(params);
    }

    @Override
    public List<TDataErr2021Entity> queryDebugger(Map<String, Object> params) {
        return baseMapper.queryDebugger(params);
    }

    @Override
    public List <Map<String,Object>> queryExceedChart(Map<String, Object> params){
        return baseMapper.queryExceedChart(params);
    }

    @Override
    public List <Map<String,Object>> queryYearChart(Map<String, Object> params){
        return baseMapper.queryYearChart(params);
    }
}
